package com.neusoft.neuiotms.dcom.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.SchedulingRuleDTO;
import com.neusoft.neuiotms.dcom.model.Classes;
import com.neusoft.neuiotms.dcom.model.SchedulingRule;
import com.neusoft.neuiotms.dcom.model.SchedulingRuleItem;
import com.neusoft.neuiotms.dcom.repository.ClassesRepository;
import com.neusoft.neuiotms.dcom.repository.SchedulingRuleItemRepository;
import com.neusoft.neuiotms.dcom.repository.SchedulingRuleRepository;

@Component
public class SchedulingRuleConverter extends AbstractConverter<SchedulingRule, SchedulingRuleDTO> {

    @Autowired
    private SchedulingRuleRepository schedulingRuleRepository;
    @Autowired
    private ClassesRepository classesRepository;
    @Autowired
    private ClassesConverter classesConverter;
    @Autowired
    private SchedulingRuleItemConverter schedulingRuleItemConverter;
    @Autowired
    private SchedulingRuleItemRepository schedulingRuleItemRepository;

    @Override
    public SchedulingRule toModel(final SchedulingRuleDTO dto) {

        SchedulingRule model;

        if (dto.isNew()) {
            model = new SchedulingRule();
        } else {
            model = this.schedulingRuleRepository.getOne(dto.getId());
        }

        model.setName(dto.getName());
        model.setGroupNumber(dto.getGroupNumber());
        model.setIsIncludedHoliday(dto.getIsIncludedHoliday());
        model.setCycleNumber(dto.getCycleNumber());

        if (dto.getClassesId() != null) {
            final Classes classes = this.classesRepository.getOne(dto.getClassesId());
            model.setClasses(classes);
        }

        final List<SchedulingRuleItem> items = model.getItems().stream().collect(Collectors.toList());

        items.forEach(k -> {
            model.getItems().remove(k);
            this.schedulingRuleItemRepository.delete(k);
        });

        dto.getItems().stream().map(s -> {
            final SchedulingRuleItem item = this.schedulingRuleItemConverter.toModel(s);
            item.setRule(model);
            return item;
        }).filter(s -> null != s).forEach(model.getItems()::add);

        return model;
    }

    @Override
    public SchedulingRuleDTO toDTO(final SchedulingRule model, final boolean forListView) {

        final SchedulingRuleDTO dto = new SchedulingRuleDTO();

        dto.setName(model.getName());
        dto.setGroupNumber(model.getGroupNumber());
        dto.setIsIncludedHoliday(model.getIsIncludedHoliday());
        dto.setId(model.getId());
        dto.setCycleNumber(model.getCycleNumber());

        if (model.getClasses() != null) {
            dto.setClassesId(model.getClasses().getId());
            dto.setClassesName(model.getClasses().getName());

            if (!forListView) {
                dto.setClasses(this.classesConverter.toDTO(model.getClasses()));

                model.getItems().stream().map(k -> this.schedulingRuleItemConverter.toDTO(k))
                        .forEach(dto.getItems()::add);
            }
        }

        return dto;
    }

}
